import io.github.bonigarcia.wdm.WebDriverManager;
import io.github.bonigarcia.wdm.managers.EdgeDriverManager;
import org.openqa.selenium.*;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

/**
 * 获取标签属性 和 弹框操作
 */

public class Demo3 {

    private EdgeDriver createDriver() {
        // 下载驱动
        EdgeDriverManager.edgedriver().setup();

        // 添加配置
        EdgeOptions options = new EdgeOptions();
        options.addArguments("--remote-allow-origins=*");

        // 获取句柄
        EdgeDriver driver = new EdgeDriver(options);


        // 打开页面
//        driver.get("https://gitee.com/zcy456");
        Common.full(driver);

        return driver;
    }

    /**
     * 1. 文本和属性的获取
     */
    public void t1() {
        EdgeDriver driver = createDriver();

//        获取属性
        WebElement element = driver.findElement(By.cssSelector("#git-header-nav > div > div > a:nth-child(4)"));
        String title = element.getAttribute("title");
//        这个获取文本
        String text = element.getText();
        /**
         * 注意
         * 标签内为属性： getAttribute 来获取
         * 标签外为文本   getTest 来获取
         *
         */
        System.out.println("得到的title: " + title + " text: " + text);

        close(driver);
    }


    private void close(EdgeDriver driver) {
//        关闭浏览器并释放对象
        driver.quit();
//        关闭当前页面并释放对象
//        driver.close();
    }

    /**
     * quit() 和 close() 的区别
     * @throws InterruptedException
     */

    public void t2() throws InterruptedException {
        EdgeDriver driver = createDriver();

        Thread.sleep(3000);
        //*[@id="popular-pinned-projects"]/div[1]/div[1]/div/a
        driver.findElement(By.xpath("//*[@id=\"popular-pinned-projects\"]/" +
                "div[1]/div[1]/div/a")).click();
        Thread.sleep(3000);
        close(driver);

    }


    /**
     * 弹窗操作
     */

    public void t3() throws InterruptedException {
        EdgeDriver driver = createDriver();

        driver.get("file:///C:/Users/%E5%91%A8%E6%AC%A1%E7%85%9C/OneDrive/%E6%A1%8C%E9%9D%A2/selenium-html/alert.html");
//        点击该链接
        driver.findElement(By.cssSelector("#tooltip")).click();


//        Thread.sleep(3000);
        Alert alert = driver.switchTo().alert();

//        Thread.sleep(3000);

        /**
         *
         * 当弹窗只有一个确定按钮时
         * accept 和 dissmiss 都可以
         */

//        alert.accept();


        alert.dismiss();

//        确认弹窗
        driver.get("file:///C:/Users/%E5%91%A8%E6%AC%A1%E7%85%9C/OneDrive/%E6%A1%8C%E9%9D%A2/selenium-html/confirm.html");
        driver.findElement(By.cssSelector("body > input[type=button]")).click();

//        alert.dismiss();
        alert.accept();



         // 输入弹窗
         driver.get("file:///C:/Users/%E5%91%A8%E6%AC%A1%E7%85%9C/OneDrive/%E6%A1%8C%E9%9D%A2/selenium-html/Prompt.html");
        WebElement element = driver.findElement(By.xpath("/html/body/input"));
        element.click();
        alert.dismiss();
        element.click();

         alert.sendKeys("我的自动化测试!");

        alert.accept();


//        // 上次文件弹窗
//        Thread.sleep(2000);
//        driver.get("file:///C:/Users/%E5%91%A8%E6%AC%A1%E7%85%9C/OneDrive/%E6%A1%8C%E9%9D%A2/selenium-html/upload.html");
//
//        Thread.sleep(2000);
//        driver.findElement(By.xpath("/html/body/div/div/input")).click();
//
//        Thread.sleep(2000);
//        alert.sendKeys("C:\\PIC\\8fd6958d-2a4c-419c-ad65-884acfec3fe3.jpg");
//
//        close(driver);
    }


    /**
     * 三种等待方式
     */
    public  void t4() throws InterruptedException {
        EdgeDriver driver = createDriver();
        driver.navigate().to("https://www.baidu.com/");
        Common.full(driver);


        driver.findElement(By.cssSelector("#kw")).sendKeys("神仙姐姐");
        driver.findElement(By.cssSelector("#su")).click();

        /**
         *  1.强制等待
         *  等待时间不确定， 效率比较低!
         */
//        Thread.sleep(1000);


        /**
         * 2. 隐式等待
         * 等待所以的元素显示， 但不能等待非元素和弹窗
         */

//        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));


        /**
         * 3. 显示等待
         * 使用 WebDriverWait 中传入 ExpectedConditions 中的条件选择来进行等待
         * 非元素和弹框都可以作用，但是作用单一的属性上， 不能作用于全体。
         */

        WebDriverWait webDriverWait = new WebDriverWait(driver,Duration.ofSeconds(3));
        webDriverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"1\"]/div/div[2]/div[1]/a/div")));
        driver.findElement(By.xpath("//*[@id=\"1\"]/div/div[2]/div[1]/a/div")).click();


        close(driver);
    }


    /**
     * 文件上传
     */

    public  void t5() {
        EdgeDriver driver = createDriver();


        driver.navigate().to("file:///C:/Users/%E5%91%A8%E6%AC%A1%E7%85%9C/OneDrive/%E6%A1%8C%E9%9D%A2/selenium-html/upload.html");

        String filePath = "C:\\PIC\\" + "8fd6958d-2a4c-419c-ad65-884acfec3fe3.jpg";
        WebElement element = driver.findElement(By.cssSelector("body > div > div > input[type=file]"));

        element.sendKeys(filePath);
        System.out.println(element.getText());
        close(driver);

    }


    /**
     * 浏览器导航: 回退和前进
     */

    public void t6() throws InterruptedException {
        EdgeDriver edgeDriver = createDriver();

        edgeDriver.navigate().to("https://www.baidu.com/");

        Thread.sleep(2000);
        edgeDriver.findElement(By.cssSelector("#kw")).sendKeys("神仙姐姐!");

        Thread.sleep(2000);

        edgeDriver.findElement(By.cssSelector("#su")).click();

        Thread.sleep(2000);

        // 进行页面回退操作
        edgeDriver.navigate().back();
        Thread.sleep(2000);

        // 进行页面前进
        edgeDriver.navigate().forward();
        Thread.sleep(2000);
        close(edgeDriver);
    }

    /**
     * 无头模式
     * 不展示给测试人员看
     */
    public void t7() throws InterruptedException {
       WebDriverManager.edgedriver().setup();

       EdgeOptions edgeOptions = new EdgeOptions();
       edgeOptions.addArguments("-headless");

       EdgeDriver edgeDriver = new EdgeDriver(edgeOptions);

        edgeDriver.navigate().to("https://www.baidu.com/");

        edgeDriver.findElement(By.cssSelector("#kw")).sendKeys("神仙姐姐!");


        edgeDriver.findElement(By.cssSelector("#su")).click();


        close(edgeDriver);
    }

    /**
     * 浏览器加载策略
     */
    public void t8() throws InterruptedException {
        WebDriverManager.edgedriver().setup();

        EdgeOptions options = new EdgeOptions();
        options.addArguments("--remote-allow-origins=*");

        /**
         * 完全不阻塞
         */
        options.setPageLoadStrategy(PageLoadStrategy.NONE);
        /**
         * 等待部分
         */
//        options.setPageLoadStrategy(PageLoadStrategy.EAGER);
        /**
         * 等待所有
         */
//                options.setPageLoadStrategy(PageLoadStrategy.NORMAL);


        WebDriver driver = new EdgeDriver(options);
        driver.get("https://www.baidu.com/");
        driver.findElement(By.cssSelector("#kw")).sendKeys("神仙姐姐");
        driver.findElement(By.cssSelector("#su")).click();


        Thread.sleep(1000);
        driver.findElement(By.xpath("//*[@id=\"1\"]/div/div[2]/div[1]/a/div")).click();

        driver.quit();
    }

}







